
'use strict';
const app = require('tcb-admin-node');
const tcb_config = {env: 'meatom04-01-2gue0fnr49a26808'};
app.init(tcb_config).auth();
const db = app.database();

let event_param
function getVerInfo(param){
  event_param = param
  return db.collection("Version").where({ 
    id: param.id,
    platform : param.platform
  }).get().then(checkVer)
}
function checkVer(res){
  if(res && res.data && res.data.length){
    return { "error" : "已存在对应版本"}
  }else{
    return addVerInfo()
  }
}
function addVerInfo(){
  let verInfo = {
    id: event_param.id,
    des: event_param.des,
    platform : event_param.platform,
    last_time: (new Date()).valueOf()
  }
  return db.collection("Version").add(verInfo).then(res => {
    return verInfo
  })
}

exports.main = async (event, context, callback) => {
  return await getVerInfo(event)
};
